-- buy_money
-- created by panyl
-- 冈布奥事件：购买金袋

-- 购买商场物品
EventMgr.register("buy_moneybuy_money", event.BUY_GOODS, function(para)
    local goodsId = para.goodsId;
    local bonus = para.bonus or {};

    -- 商品基本信息
    local dbase = MarketM.query(goodsId, "dbase") or {};

    if dbase["slime_event"] ~= "buy_money" then
        -- 不是该子规则关注的事件
        return;
    end

    -- 消费的钻石
    local gem = MarketM.queryGoodsPrice(goodsId)["gem"] or 0;

    -- 获得金币
    local money = 0;
    local allBonus = {};
    if #bonus > 0 and type(bonus[1]) == "number" then
        allBonus = { bonus, };
    end
    for _, bonus in pairs(allBonus) do
        if bonus[1] == 2 and bonus[2] == "money" then
            money = money + bonus[3];
        end
    end

    local arr = table.deepcopy(SlimeEventM.getIdsByRule("buy_money"));

    -- 随机顺序来
    local num = #arr;
    for i = 1, num do
        repeat
            local index = RandomFactoryM.myRandom(ME.user) % num + 1;

            local id = arr[index];

            -- 用过之后就放到最后面
            arr[index] = arr[num];
            num = num - 1;

            local dbase = SlimeEventM.query(id, "dbase");

            -- 概率触发
            local rand = RandomFactoryM.myRandom(ME.user) % 100;
            local petId = dbase["pet_id"];

            if type(petId) ~= "number" then
                break;
            end

            pet = ME.user.pets[petId];
            if not pet then
                break;
            end

            local rate = 6;
            if rand < rate then
                -- 抽取到了
                SlimeEventM.triggerEvent(id, {["gem"] = gem, ["money"] = money, });

                -- 只能触发一个事件
                return;
            end
        until true;
    end
end);

return {
};
